package com.hue.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.hue.service.VodService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {
    @Value("${aliyun.vod.keyid}")
    private String keyid;
    @Value("${aliyun.vod.keysecret}")
    private String keysecret;

    @Override
    public String upload(MultipartFile file) {
        String videoId = "";
        try {
            String filename = file.getOriginalFilename();
            filename = System.currentTimeMillis() + filename;
            String title = filename.split("[.]")[0];
            videoId = testUploadURLStream(keyid, keysecret, title, filename, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return videoId;
    }

    @Override
    public boolean delete(String videoId) {
        DefaultAcsClient client = null;
        try {
            client = initVodClient(keyid, keysecret);
            //批量删除视频的请求
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            //删除视频
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean deletes(List<String> videoIds) {
        DefaultAcsClient client = null;
        try {
            if (videoIds != null || videoIds.size() > 0) {
                //拼接视频ids
                String ids = "";
                for (String id : videoIds) {
                    ids += id + ",";
                }
                //删除最后一个逗号
                ids = ids.substring(0, ids.length() - 1);
                client = initVodClient(keyid, keysecret);
                //批量删除视频的请求
                DeleteVideoRequest request = new DeleteVideoRequest();
                request.setVideoIds(ids);
                //删除视频
                client.getAcsResponse(request);
            }
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /*
        使用流式上传文件,返回上传成功后视频的ID
     */
    private String testUploadURLStream(String accessKeyId, String accessKeySecret, String title, String fileName, InputStream inputStream) {
        UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        if (response.isSuccess()) {
            String videoId = response.getVideoId();
            return videoId;
        }
        return null;
    }

    //连接vod
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入区域
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
}
